<template>
    <div>
        <el-dialog
            :title="title"
            :close-on-click-modal="false"
            v-dialogDrag
            :visible.sync="editVisible"
            @open="getBusinessClient"
        >
            <el-form
                v-loading="loading"
                label-width="99px"
                ref="editForm"
                :model="editForm">

                <el-form-item label="账户类型:" prop="financeAccountType">
                    <el-radio-group :disabled="method==='info'" v-model="editForm.companyAccountType"
                                    @change="selectRadio(editForm.companyAccountType)">
                        <el-radio :label="item.value" :key="item.value"
                                         v-for="item in  $dictUtils.getDictList('financeAccountType')">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!--业务账户form-->
                <el-form-item label="活动账户:" v-if="editForm.companyAccountType==='1'" prop="activityAccount">
                    <el-input disabled v-model="editForm.activityAccount"></el-input>
                </el-form-item>
                <el-form-item label="账户余额:" v-if="editForm.companyAccountType==='1'" prop="accountMoney">
                    <el-input disabled v-model="editForm.accountMoney"></el-input>
                </el-form-item>

                <el-form-item label="产品单价:" v-if="editForm.companyAccountType==='1'" prop="productPrice1">
                    <el-input disabled v-model="editForm.productPrice1"></el-input>
                </el-form-item>
                <el-form-item label="通知数量:" v-if="editForm.companyAccountType==='1'" prop="noticeQuantity1" :rules="[
                              {required: true, message:'请输入通知数量', trigger:'blur'},{validator:checkNoticeQuantity1, trigger:'blur'}
                             ]">
                    <el-input class="adjustInputAppend appendLess" :disabled="method==='info'" v-model="editForm.noticeQuantity1">
                        <span slot="suffix">吨</span>
                    </el-input>
                </el-form-item>
                <el-form-item label="通知金额:" v-if="editForm.companyAccountType==='1'" prop="noticeMoney1">
                    <el-input disabled v-model="editForm.noticeMoney1"></el-input>
                </el-form-item>
                <!--活动账户form-->
                <el-form-item label="业务编号:" v-if="editForm.companyAccountType==='2'" prop="codes" :rules="[
                  {required: true, message:'请选择业务', trigger:'change'}
                 ]">
                    <el-input :disabled="method==='info'" class="selectInput" v-model="editForm.codes"
                              @focus="openBusinessClient">
<!--                              @focus="getBusinessClient"-->
<!--                        <template slot="append"><span @click="openBusinessClient">选择</span></template>-->
                    </el-input>
                </el-form-item>
                <el-form-item label="客户名称:" v-if="editForm.companyAccountType==='2'" prop="companyName">
                    <el-input disabled v-model="editForm.companyName"></el-input>
                </el-form-item>
                <el-form-item label="产品名称:" v-if="editForm.companyAccountType==='2'" prop="productName">
                    <el-input disabled v-model="editForm.productName"></el-input>
                </el-form-item>
                <el-form-item label="产品单价:" v-if="editForm.companyAccountType==='2'" prop="productPrice">
                    <el-input disabled v-model="editForm.productPrice"></el-input>
                </el-form-item>
                <el-form-item label="可用余量:" v-if="editForm.companyAccountType==='2'" prop="surpQuantity">
                    <el-input disabled v-model="editForm.surpQuantity"></el-input>
                </el-form-item>
                <el-form-item label="通知数量:" v-if="editForm.companyAccountType==='2'" prop="noticeQuantity" :rules="[
                              {required: true, message:'请输入通知数量', trigger:'blur'},
                              {validator:checkNoticeQuantity, trigger:'blur'}
                             ]">
                    <el-input class="adjustInputAppend appendLess" :disabled="method==='info'" v-model="editForm.noticeQuantity">
                        <span slot="suffix">吨</span>
                    </el-input>
                </el-form-item>
                <el-form-item label="通知金额:" v-if="editForm.companyAccountType==='2'" prop="noticeMoney">
                    <el-input disabled v-model="editForm.noticeMoney"></el-input>
                </el-form-item>
                <!--信用账户-->
                <el-form-item label="客户名称:" v-if="editForm.companyAccountType==='3'" prop="companyName3">
                    <el-input disabled v-model="editForm.companyName3"></el-input>
                </el-form-item>
                <el-form-item label="产品名称:" v-if="editForm.companyAccountType==='3'" prop="productName3">
                    <el-input disabled v-model="editForm.productName3"></el-input>
                </el-form-item>

                <el-form-item label="产品单价:" v-if="editForm.companyAccountType==='3'" prop="productPrice3">
                    <el-input disabled v-model="editForm.productPrice3"></el-input>
                </el-form-item>
                <el-form-item label="可用信用量:" v-if="editForm.companyAccountType==='3'" prop="creditNumber">
                    <el-input disabled v-model="editForm.creditNumber"></el-input>
                </el-form-item>
                <el-form-item label="通知数量:" v-if="editForm.companyAccountType==='3'" prop="noticeQuantity3" :rules="[
                              {required: true, message:'请输入通知数量', trigger:'blur'},
                              {validator:checkNoticeQuantity3, trigger:'blur'}
                             ]">
                    <el-input class="adjustInputAppend appendLess" :disabled="method==='info'" v-model="editForm.noticeQuantity3">
                        <span slot="suffix">吨</span>
                    </el-input>
                </el-form-item>
                <el-form-item label="通知金额:" v-if="editForm.companyAccountType==='3'" prop="noticeMoney3">
                    <el-input disabled v-model="editForm.noticeMoney3"></el-input>
                </el-form-item>
                <!--欠款账户-->
                <el-form-item label="客户名称:" v-if="editForm.companyAccountType==='4'" prop="companyName4">
                    <el-input disabled v-model="editForm.companyName4"></el-input>
                    <p class="account" v-if="editForm.companyAccountType==='4'">当前欠款合计{{ editForm.debtMoney }}元</p>
                </el-form-item>

                <el-form-item label="产品单价:" v-if="editForm.companyAccountType==='4'" prop="productPrice4">
                    <el-input disabled v-model="editForm.productPrice4"></el-input>
                </el-form-item>
                <el-form-item label="通知数量:" v-if="editForm.companyAccountType==='4'" prop="noticeQuantity4" :rules="[
                              {required: true, message:'请输入通知数量', trigger:'blur'},
                              {validator:checkNoticeQuantity4, trigger:'blur'}
                             ]">
                    <el-input class="adjustInputAppend appendLess" :disabled="method==='info'" v-model="editForm.noticeQuantity4">
                        <span slot="suffix">吨</span>
                    </el-input>
                </el-form-item>
                <el-form-item label="通知金额:" v-if="editForm.companyAccountType==='4'" prop="noticeMoney4">
                    <el-input disabled v-model="editForm.noticeMoney4"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
<!--        <div class="footer-input">-->
<!--            <span slot="footer" class="dialog-footer">-->
        <el-button size="small" :disabled="editForm.flowStateus===2" type="primary" @click="doSubmit(false)"
                   v-noMoreClick>确定</el-button>
        <el-button size="small" :disabled="editForm.flowStateus===2&&editForm.taskKey!=='UserUpdate'" type="warning"
                   @click="doSubmit(true)" v-noMoreClick>提交</el-button>
        <el-button size="small" @click="close()">取消</el-button>
<!--      </span>-->
          <!--<el-button size="small" type="primary" @click="doSubmit()" v-noMoreClick
          >确定</el-button>
            <el-button size="small" :disabled="editForm.flowStateus===2&&editForm.taskKey!=='UserUpdate'" type="warning"
                       @click="doSubmit(true)" v-noMoreClick>提交</el-button>
        <el-button size="small" @click="close()">取消</el-button>-->

<!--        </div>-->

      </span>
        </el-dialog>
        <!--客户选择弹窗-->
        <BusinessCodesDialog ref="BusinessCodesDialog" @getCompanyData="getBusinessCodesData">

        </BusinessCodesDialog>
    </div>
</template>
<script>
    import {save, getById, selectAccount} from '@/api/business/BusinessWeight.js';
    import {adjustTheBusiness} from '@/api/business/business.js';
    import BusinessCodesDialog from '@/components/dialog/businessCodesDialog'

    export default {
        components: {
            BusinessCodesDialog
        },
        data() {
            return {
                loading: false,
                areaLabel: {},
                method: '',
                title: '',
                editForm: {
                    companyName1: '',
                    companyName3: '',
                    companyName4: '',
                    productName1: '',
                    productName3: '',
                    productName4: '',
                    productPrice1: '',
                    productPrice3: '',
                    productPrice4: '',
                    noticeQuantity1: '',
                    noticeQuantity3: '',
                    noticeQuantity4: '',
                    noticeMoney1: '',
                    noticeMoney3: '',
                    noticeMoney4: '',
                    debtMoney: '',
                    id: '',
                    codes: '',
                    truckTailId: '',
                    weightTime: '',
                    flowStateus: '',
                    companyAccountType: '2',
                    activityAccount: '',
                    accountMoney: '',
                    creditNumber: '',
                    businessId: '',
                    productPrice: '',
                    surpQuantity: '',
                    quantity: '',
                    money: '',
                    noticeMoney: '',
                    noticeQuantity: '',
                    nowPrice: '',
                    paymentTime: '',
                    ruleType: '',
                    paymentState: '',
                    ratio: ''
                },
                editVisible: false,
                detailsVisible: false,
                tableData1: [],
                page: {
                    pageSize: 10,
                    pageNo: 1,
                    total: 0
                },
                props: 1,
                dialogVisible: false,
                businessObj: {},
                businessWeightObj: {},
                query: ''
            }
        },
        methods: {
        	handleClose(done) {
						this.$Msg.confirm({
							title:'您确定要关闭吗？',
							content:this.GLOBAL.FORM_CLOSE_TIPS,
							confirmCall:()=>{
								done();
							}
						})
					},
            selectRadio(value) {
                this.editForm.companyName1 = ''
                this.editForm.companyName3 = ''
                this.editForm.companyName4 = ''
                this.editForm.productName1 = ''
                this.editForm.productName3 = ''
                this.editForm.productName4 = ''
                this.editForm.productPrice1 = ''
                this.editForm.productPrice3 = ''
                this.editForm.productPrice4 = ''
                this.editForm.noticeQuantity1 = ''
                this.editForm.noticeQuantity3 = ''
                this.editForm.noticeQuantity4 = ''
                this.editForm.noticeMoney1 = ''
                this.editForm.noticeMoney3 = ''
                this.editForm.noticeMoney4 = ''
                this.editForm.id = ''
                this.editForm.codes = ''
                this.editForm.truckTailId = ''
                this.editForm.weightTime = ''
                this.editForm.companyAccountType = value
                this.editForm.activityAccount = ''
                this.editForm.accountMoney = ''
                this.editForm.businessId = ''
                this.editForm.creditNumber = ''
                this.editForm.debtMoney = ''
                this.editForm.surpQuantity = ''
                this.editForm.productPrice = ''
                this.editForm.quantity = ''
                this.editForm.nowPrice = ''
                this.editForm.money = ''
                this.editForm.noticeQuantity = ''
                this.editForm.noticeMoney = ''
                this.editForm.paymentTime = ''
                this.editForm.ruleType = ''
                this.editForm.paymentState = ''
                this.editForm.ratio = ''

                this.selectAccount(value);
                console.log(value);

            },
            selectAccount(value) {
                selectAccount({
                    id: this.method === 'info' || this.method === 'edit' ? this.businessWeightObj.id : '',
                    companyAccountType: value,
                    companyId: this.businessObj.business.companyId,
                    businessId: this.businessObj.business.id,
                    dataType: this.businessObj.business.dataType,
                    payMode: this.businessObj.payMode,
                    productId: this.businessObj.product.id,
                    pageSize: this.page.pageSize,
                    pageNo: this.page.pageNo
                }).then((res) => {
                    if (res.data.code === '200') {
                        if (value === '1') {
                            this.editForm.productPrice1 = res.data.result.nowPrice;
                            this.editForm.accountMoney = res.data.result.accountMoney;
                            this.editForm.activityAccount = res.data.result.companyName + ' | ' + this.$dictUtils.getDictLabel("payMode", res.data.result.payMode, "") + ' | ' + this.$dictUtils.getDictLabel("dataType", res.data.result.dataType, "");
                            if (this.method === 'info' || this.method === 'edit') {
                                this.editForm.noticeQuantity1 = res.data.result.noticeNumber;
                                this.editForm.noticeMoney1 = res.data.result.noticeMoney;
                            }
                        } else if (value === '2') {
                            this.editForm.productPrice = res.data.result.nowPrice;
                            this.editForm.accountMoney = res.data.result.accountMoney;
                            this.editForm.codes = res.data.result.codes;
                            this.editForm.companyName = res.data.result.companyName;
                            this.editForm.productName = res.data.result.productNames;
                            this.editForm.surpQuantity = res.data.result.surpQuantity;
                            if (this.method === 'info' || this.method === 'edit') {
                                this.editForm.noticeQuantity = res.data.result.noticeNumber;
                                this.editForm.noticeMoney = res.data.result.noticeMoney;
                            }
                        } else if (value === '3') {
                            this.editForm.companyName3 = res.data.result.companyName;
                            this.editForm.productName3 = res.data.result.productNames;
                            this.editForm.productPrice3 = res.data.result.nowPrice;
                            if (res.data.result.creditNumber) {
                                this.editForm.creditNumber = res.data.result.creditNumber;
                            }else {
                                this.editForm.creditNumber = '0'
                            }
                            if (this.method === 'info' || this.method === 'edit') {
                                this.editForm.noticeQuantity3 = res.data.result.noticeNumber;
                                this.editForm.noticeMoney3 = res.data.result.noticeMoney;
                            }
                        } else if (value === '4') {
                            this.editForm.companyName4 = res.data.result.companyName;
                            this.editForm.productPrice4 = res.data.result.nowPrice;
                            this.editForm.debtMoney = res.data.result.debtMoney;
                            if (this.method === 'info' || this.method === 'edit') {
                                this.editForm.noticeQuantity4 = res.data.result.noticeNumber;
                                this.editForm.noticeMoney4 = res.data.result.noticeMoney;
                            }
                        }

                    }
                });
            },
            // 客户选择弹窗
            getBusinessClient() {
                this.$nextTick(() => {
                    this.$refs['BusinessCodesDialog'].getCompanyList(this.page, this.businessObj);
                    // this.editForm = {}
                });
                if (this.editForm.companyAccountType === '2') {

                }
            },
            openBusinessClient() {
                this.$refs['BusinessCodesDialog'].dialogVisible = true
                this.$refs['BusinessCodesDialog'].holder = '请输入业务编号'
            },
            // 客户选择数据回显
            getBusinessCodesData(row) {
                this.editForm.codes = row.codes
                this.editForm.businessId = row.id
                this.editForm.companyName = row.companyName
                this.editForm.companyId = row.companyId
                this.editForm.productName = row.productNames
                this.editForm.productId = row.productId
                this.editForm.surpQuantity = row.surpQuantity
                this.editForm.productPrice = row.nowPrice
                this.dialogVisible = false
                console.log(row)
            },
            select(e) {
                this.editForm.companyName = e.names
                this.editForm.companyId = e.id
                this.dialogVisible = false
                console.log(e)
            },
            init(method, weight, businessWeight) {
                if (method === 'add') {
                    this.businessObj = weight
                    console.log(this.businessObj);
                    this.editVisible = true
                    this.$nextTick(() => {
                        this.$refs.editForm.resetFields()
                        // this.editForm = {}
                    })
                    this.editForm.id = ''
                    this.editForm.truckTailId = ''
                    this.editForm.weightTime = ''
                    this.editForm.companyAccountType = '2'
                    this.editForm.businessId = ''
                    this.editForm.productPrice = ''
                    this.editForm.quantity = ''
                    this.editForm.nowPrice = ''
                    this.editForm.money = ''
                    this.editForm.paymentTime = ''
                    this.editForm.ruleType = ''
                    this.editForm.paymentState = ''
                    this.editForm.ratio = ''
                    this.title = '新增账务通知'
                    this.method = method
                    this.visible = true
                    this.loading = false
                } else if (method === 'info') {
                    this.businessObj = weight
                    this.businessWeightObj = businessWeight
                    this.editVisible = true
                    this.editForm.id = ''
                    this.editForm.truckTailId = ''
                    this.editForm.weightTime = ''
                    this.editForm.companyAccountType = businessWeight.companyAccountType + '';
                    console.log(businessWeight);
                    this.editForm.businessId = ''
                    this.editForm.productPrice = ''
                    this.editForm.quantity = ''
                    this.editForm.nowPrice = ''
                    this.editForm.money = ''
                    this.editForm.paymentTime = ''
                    this.editForm.ruleType = ''
                    this.editForm.paymentState = ''
                    this.editForm.ratio = ''
                    this.title = '详情'
                    this.method = method
                    this.visible = true
                    this.loading = false
                    this.selectAccount(this.editForm.companyAccountType);
                } else if (method === 'edit') {
                    this.businessObj = weight
                    this.businessWeightObj = businessWeight
                    this.editForm.companyAccountType = businessWeight.companyAccountType + '';
                    this.editVisible = true
                    this.method = method
                    this.title = '修改账务通知'
                    console.log(weight.id)
                    this.selectAccount(this.editForm.companyAccountType);
                    /*this.$nextTick(() => {
                        this.loading = true
                        if (method === 'edit' || method === 'view') { // 修改或者查看
                            getById({id: this.editForm.id}).then((res) => {
                                if (res.data.code === '200') {
                                    this.editForm = res.data.result
                                    console.log(this.editForm)
                                    this.loading = false
                                } else {
                                    this.loading = false
                                    this.visible = false
                                    this.$message({
                                        type: 'error',
                                        message: res.data.message
                                    })
                                }
                            })
                        }
                    })*/
                    this.visible = true
                    this.loading = false
                }

            },
            close() {
                this.editVisible = false
            },

            // 信用账户校验通知数量
            checkNoticeQuantity3(rule, value, callback) {
                this.editForm.noticeMoney3 = value * this.editForm.productPrice3;
                if (value > this.editForm.creditNumber) {
                    callback(new Error('不允许超过可用信用量'))
                } else {
                    callback()
                }
            },
            // 活动账户通知金额
            checkNoticeQuantity1(rule, value, callback) {
                console.log(value);
                this.editForm.noticeMoney1 = value * this.editForm.productPrice1;
                console.log(this.editForm.noticeMoney1)
                callback()
                /*if (value > this.editForm.creditNumber) {
                    callback(new Error('不允许超过可用信用量'))
                } else {
                    callback()
                }*/
            },
            // 欠款账户通知金额
            checkNoticeQuantity4(rule, value, callback) {
                this.editForm.noticeMoney4 = value * this.editForm.productPrice4;
                /*if (value > this.editForm.creditNumber) {
                    callback(new Error('不允许超过可用信用量'))
                } else {
                    callback()
                }*/
            },
            // 校验通知数量
            checkNoticeQuantity(rule, value, callback) {
                this.editForm.noticeMoney = value * this.editForm.productPrice;
                if (value > this.editForm.surpQuantity) {
                    callback(new Error('不允许超过可用余量'))
                } else {
                    callback()
                }
            },
            doSubmit(isFlowable) {
                this.$refs['editForm'].validate((valid) => {
                    if (valid) {
                        this.$set(this.editForm, 'isFlowable', isFlowable)
                        if (this.editForm.companyAccountType === '2') {
                            save({
                                id: this.method === 'edit' ? this.businessWeightObj.id : '',
                                truckTailId: this.businessObj.truckTrailId,
                                productId: this.businessObj.product.id,
                                companyAccountType: this.editForm.companyAccountType,
                                businessId: this.businessObj.business.id,
                                productPrice: this.editForm.productPrice,
                                quantity: this.editForm.noticeQuantity,
                                money: this.editForm.noticeMoney,
                                weightTime: this.businessObj.gross.weightTime
                            }).then((res) => {
                                if (res.data.code === '200') {
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    })
                                    this.editVisible = false
                                    this.$emit('refreshDataList')
                                }
                            });
                        } else if (this.editForm.companyAccountType === '1') {
                            save({
                                id: this.method === 'edit' ? this.businessWeightObj.id : '',
                                truckTailId: this.businessObj.truckTrailId,
                                productId: this.businessObj.product.id,
                                companyAccountType: this.editForm.companyAccountType,
                                businessId: this.businessObj.business.id,
                                productPrice: this.editForm.productPrice1,
                                quantity: this.editForm.noticeQuantity1,
                                money: this.editForm.noticeMoney1,
                                weightTime: this.businessObj.gross.weightTime
                            }).then((res) => {
                                if (res.data.code === '200') {
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    })
                                    this.editVisible = false
                                    this.$emit('refreshDataList')
                                }
                            });
                        } else if (this.editForm.companyAccountType === '3') {
                            save({
                                id: this.method === 'edit' ? this.businessWeightObj.id : '',
                                truckTailId: this.businessObj.truckTrailId,
                                productId: this.businessObj.product.id,
                                companyAccountType: this.editForm.companyAccountType,
                                businessId: this.businessObj.business.id,
                                productPrice: this.editForm.productPrice3,
                                quantity: this.editForm.noticeQuantity3,
                                money: this.editForm.noticeMoney3,
                                weightTime: this.businessObj.gross.weightTime
                            }).then((res) => {
                                if (res.data.code === '200') {
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    })
                                    this.editVisible = false
                                    this.$emit('refreshDataList')
                                }
                            });
                        } else if (this.editForm.companyAccountType === '4') {
                            save({
                                id: this.method === 'edit' ? this.businessWeightObj.id : '',
                                truckTailId: this.businessObj.truckTrailId,
                                productId: this.businessObj.product.id,
                                companyAccountType: this.editForm.companyAccountType,
                                businessId: this.businessObj.business.id,
                                productPrice: this.editForm.productPrice4,
                                quantity: this.editForm.noticeQuantity4,
                                money: this.editForm.noticeMoney4,
                                weightTime: this.businessObj.gross.weightTime
                            }).then((res) => {
                                if (res.data.code === '200') {
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    })
                                    this.editVisible = false
                                    this.$emit('refreshDataList')
                                }
                            });
                        }


                    }
                })
            }
        },
        mounted() {

        }
    }
</script>
<style lang="scss" scoped>
    ::v-deep .el-dialog {
        .el-form {
            .el-radio-group{
                .el-radio{
                    margin-right: 15px;
                    .el-radio__label{
                        font-size: 16px;
                        padding-left: 10px;
                    }
                }
                .el-radio:last-child{
                    margin-right: 0;
                }
            }
            .account{
                font-size: 12px;
                color: #FF3E3E;
                line-height: 20px;
                position: absolute;
            }
            .el-input__inner {
                width: 320px;
            }

            .inputAppend-wrapper {
                .el-input__inner {
                    width: 282px;
                }
            }
        }
    }
</style>
